package com.zhiyizhen.handheld.component.execute.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zhiyizhen.handheld.R;
import com.zhiyizhen.handheld.component.execute.bean.executedetail.GroupListItem;
import com.zhiyizhen.handheld.component.main.bean.EnumFieldBean;

import java.util.List;

/**
 * description
 */
public class ShuyeUnitLayout extends LinearLayout {

    private TextView seqTv;
    private TextView drugInfoTv;
    private TextView pishiTv;
    private TextView dosageTv;
    private TextView totalCountTv;
    private TextView remarkTv;


    public ShuyeUnitLayout(Context context, int seq, GroupListItem.DetailListBean detailListBean, List<EnumFieldBean> skinTestEnums) {
        super(context);
        initView(context);
        String drugInfo = getStringBuilder(detailListBean).toString();
        String dosage = detailListBean.getDosage() + detailListBean.getTotalOutQtyUnitText();

        String skinTest = "--";
        for (int i = 0; i < skinTestEnums.size(); i++) {
            if (TextUtils.equals(detailListBean.getSkinTest(), skinTestEnums.get(i).getValue())) {
                skinTest = skinTestEnums.get(i).getText();
            }
        }
        setData(seq, drugInfo, skinTest, dosage, detailListBean.getTotalOutQty() + detailListBean.getTotalOutQtyUnitText(), detailListBean.getRemark());
    }

    @NonNull
    private static StringBuilder getStringBuilder(GroupListItem.DetailListBean detailListBean) {
        StringBuilder drugInfoSb = new StringBuilder();
        drugInfoSb.append(detailListBean.getDrugName());
        drugInfoSb.append(" ");
        if(detailListBean.getDrugDosage() != null) {
            drugInfoSb.append(detailListBean.getDrugDosage())
                    .append(detailListBean.getDrugDosageUnitText())
                    .append("*");
        }
        drugInfoSb.append(detailListBean.getMinPackQty());
        drugInfoSb.append(detailListBean.getMinPackUnitText());
        drugInfoSb.append("/")
                .append(detailListBean.getPackUnitText());
        return drugInfoSb;
    }

    public ShuyeUnitLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    private void initView(Context context) {
        inflate(context, R.layout.view_shuye_seq, this);
        setOrientation(LinearLayout.HORIZONTAL);
        seqTv = findViewById(R.id.seqTv);
        drugInfoTv = findViewById(R.id.drugInfoTv);
        pishiTv = findViewById(R.id.pishiTv);
        dosageTv = findViewById(R.id.dosageTv);
        totalCountTv = findViewById(R.id.totalCountTv);
        remarkTv = findViewById(R.id.remarkTv);
    }

    private void setData(int seq, String drugInfo, String pishi, String dosage, String totalCount, String remark) {
        seqTv.setText(String.valueOf(seq));
        drugInfoTv.setText(drugInfo);
        pishiTv.setText(pishi);
        dosageTv.setText(dosage);
        totalCountTv.setText(totalCount);
        remarkTv.setText(remark);
    }

    public void hideDivider() {
        findViewById(R.id.dividerView).setVisibility(GONE);
    }
    public void showDivider() {
        findViewById(R.id.dividerView).setVisibility(VISIBLE);
    }



}